<?php

namespace App\Service\CallingCards;

use App\Models\FeishuCallingCard\FeishuCallingCardFranchisee;
use App\Service\Common\OssService;
use Illuminate\Support\Str;
use Intervention\Image\Facades\Image;
use Kuafu\File\FileUtil;

class MerchantShareImageService extends ShareImageService
{
    protected static $backgroundFile = '';
    protected static $wechatQrcodeFile = '';
    protected static $avatarFile = '';

    public static function prepareMerchantResource(string $backgroundUrl, int $shopId, int $uid, string $avatar)
    {
        if (!strlen($backgroundUrl)) {
            $backgroundUrl = ShareImageService::DEFAULT_BG;
        }
        if (!self::checkSha256('fx', static::FX)) {
            // 覆盖的白色底部那一层
            self::downloadFile(static::FX, static::tempFileName('fx'));
        }
        if (!self::checkSha256('font', static::FONT)) {
            self::downloadFile(static::FONT, static::tempFileName('font'));
        }
        static::$backgroundFile   = static::tempFileName(Str::random());
        static::$wechatQrcodeFile = static::tempFileName(Str::random());
        static::$avatarFile       = static::tempFileName(Str::random());
        static::downloadFile($backgroundUrl, static::$backgroundFile);

        $fileContent = WechatService::genCallingQrCode('u' . $uid . 's' . $shopId);
        file_put_contents(static::$wechatQrcodeFile, $fileContent);
        static::downloadFile($avatar, static::$avatarFile);
        $image = Image::make(static::$backgroundFile);
        $width = $image->width();
        static::$bgHeight = $image->height();
        if (750 != $width) {
            static::$bgHeight = round(static::$bgHeight / $width * 750);
            Image::make(static::$backgroundFile)
                ->resize(750, static::$bgHeight)
                ->save(static::$backgroundFile);
        }
        Image::make(static::$avatarFile)
            ->resize(92, 92)
            ->save(static::$avatarFile);
        Image::make(static::$wechatQrcodeFile)
            ->resize(248, 248)
            ->save(static::$wechatQrcodeFile);
    }

    protected static function makePoster(string $name, string $mobile, string $position)
    {
        $tmpFile = sys_get_temp_dir() . '/' . Str::random(10) . '.jpg';
        $image = Image::canvas(750, static::$bgHeight + 248)
            ->insert(static::$backgroundFile)
            ->insert(static::$avatarFile, 'top-left', 40, static::$bgHeight - 182 + 248)
            ->insert(static::tempFileName('fx'), 'top-left', 0, static::$bgHeight + 248 - 356)
            ->text($name, 156, static::$bgHeight - 142 + 248, function ($font) {
                $font->file(static::tempFileName('font'));
                $font->size(32);
                $font->color('#3A3A3A');
            })
            ->text($mobile, 156, static::$bgHeight - 96 + 248, function ($font) {
                $font->file(static::tempFileName('font'));
                $font->size(26);
                $font->color('#979797');
            });
        $image->text($position, 156, static::$bgHeight - 56 + 248, function ($font) {
            $font->file(static::tempFileName('font'));
            $font->size(26);
            $font->color('#979797');
        });
        $image->insert(static::$wechatQrcodeFile, 'top-left', 456, static::$bgHeight + 248 - 338)
            ->save($tmpFile);
        return $tmpFile;
    }

    public static function makeShareImage(int $uid, string $backgroundUrl, string $name, string $mobile, string $position, string $avatar, int $shopId): array
    {
        ini_set('memory_limit','2048M');
        static::prepareMerchantResource($backgroundUrl, $shopId, $uid, $avatar);
        $tmpFile = static::makePoster($name, $mobile, $position);
        $arr = OssService::uploadFile($tmpFile);
        @unlink($tmpFile);
        @unlink(static::$backgroundFile);
        @unlink(static::$wechatQrcodeFile);
        @unlink(static::$avatarFile);
        return $arr;
    }

    public static function uploadBackground(FeishuCallingCardFranchisee $franchisee, string $path)
    {
        if (!self::checkSha256('fx', static::FX)) {
            // 覆盖的白色底部那一层
            self::downloadFile(static::FX, static::tempFileName('fx'));
        }
        $tmp = sys_get_temp_dir() . '/' . Str::random();
        @unlink($tmp);
        static::downloadFile($path, $tmp);
        $image = Image::make($tmp);
        $width = $image->width();
        $height = $image->height();
        $image->resize(750, intval($height / $width * 750))->save($tmp);
        $image = null;
        $arr = FileUtil::uploadFile($tmp, FileUtil::guessSuffix($path));
        $franchisee->backgroundUrl = $arr['path'];
        $bgHeight = Image::make($tmp)->height();
        $targetFile = sys_get_temp_dir() . '/' . Str::random() . '.jpg';
        Image::canvas(750, $bgHeight + 248)
            ->insert($tmp)
            ->insert(static::tempFileName('fx'), 'top-left', 0, $bgHeight + 248 - 356)
            ->save($targetFile);
        $arr2 = OssService::uploadFile($targetFile, '.jpg');
        $franchisee->wechatPreviewUrl = $arr2['path'];
        $franchisee->save();
    }
}
